<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller
{

    public function index()
    {
        //check whether right request
        if (isset($_GET['token'])) {
            if ($_GET['token'] == 12345) {
                header("Content-Type: application/json");
                $this->load->model('deal');
                $data = $this->deal->get_hot_deals();
                if (!empty($data)) {
                    $this->dataResponse('200', 'deals found', $data);
                } else {
                    $this->dataResponse('200', 'deals not found', NULL);
                }
            } else {
                $this->dataResponse('400', 'Invalid Request', NULL);
            }
        } else //invalid request
        {
            $this->dataResponse('400', 'Invalid Request', NULL);
        }
    }

    function dataResponse($status, $status_massage, $data)
    {
        header("HTTP/1.1 $status $status_massage ");

        $response['status'] = $status;
        $response['status_message'] = $status_massage;
        $response['data'] = $data;
        $json_response = json_encode($response);
        echo $json_response;
    }

    public function security()
    {
        $this->dataResponse('200', '', $this->security->csrf_token_name);
        $this->security->csrf_hash;
    }
}
